// pages/user/user.js
const app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        nickName: '',
        avatarUrl: '',
        user: [],

        school_modal: '',
        select_school_id: '',
        select_school_name: '',
        school_id: '',
        school_name: '',

        role_modal: '',
        select_role: '',

        study_count:'',
        study_time: '',

        teacher_count:'',
        teacher_time: '',

        message_list:[],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        this.getUser();
        this.getMessageList()
    },

    onLogin: function() {
        this.getUser();
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    },
    
    //请先登录
    getUser: function () {
        let _this = this;
        app.beLogin(function () {
            app._get('/api/login/user/getUserInfo', {
            }, function (response) {
                let data = response.data.data;
                if (response.data.code == 200) {
                    _this.setData({
                        user: data
                    })
                    app.globalData.user = data;
                }
                //如果登录后没有绑定学校
                if(data.school_id==null||data.school_name==null)
                {
                    _this.showSchool();
                }
                //如果登录后没有绑定角色
                if(data.role==null)
                {
                    _this.showRole();
                }
                if(_this.data.user.role==1){
                    //学生的话计算时长与次数
                    _this.getStudentTimeCount();
                }else if(_this.data.user.role==2){
                    //教师的话计算时长与次数
                    _this.getTeacherTimeCount();
                }
                else if(_this.data.user.role==3){
                    //班主任的话计算学生数与课程数
                    _this.getClassmasterTimeCount();
                }
                else if(_this.data.user.role==4){
                    //校长的话计算学生数与班级数
                    _this.getSchoolmasterTimeCount();
                }
                console.log(_this.data.user)
            })
        });
    },

    // 学生的话计算时长与次数
    getStudentTimeCount: function(){
        let _this = this;
        app._get('/api/login/student/getTimeCount', {
        }, function (response) {
            let data = response.data.data;
            if (response.data.code == 200) {
                _this.setData({
                    study_count: data.count,
                    study_time: data.time
                })
            }
        })
    },

    // 教师的话计算时长与次数
    getTeacherTimeCount: function(){
        let _this = this;
        app._get('/api/login/teacher/getTimeCount', {
        }, function (response) {
            let data = response.data.data;
            if (response.data.code == 200) {
                _this.setData({
                    teacher_count: data.count,
                    teacher_time: data.time
                })
            }
        })
    },

    
    // 班主任的话计算学生数与课程数与缺勤率
    getClassmasterTimeCount: function(){
        let _this = this;
        app._get('/api/login/classmaster/getTimeCount', {
        }, function (response) {
            let data = response.data.data;
            if (response.data.code == 200) {
                _this.setData({
                    student_count: data.student_count,
                    course_count: data.course_count,
                    attendance_rate: data.attendance_rate,
                })
            }
        })
    },

    // 校长的话计算学生数与班级数与缺勤率
    getSchoolmasterTimeCount: function(){
        let _this = this;
        app._get('/api/login/schoolmaster/getTimeCount', {
        }, function (response) {
            let data = response.data.data;
            if (response.data.code == 200) {
                _this.setData({
                    student_count: data.student_count,
                    class_count: data.class_count,
                    attendance_rate: data.attendance_rate,
                })
            }
        })
    },

    //课程
    toCourse: function(){
        wx.switchTab({
            url: "/pages/course/course"
        })
    },

    //完善信息
    editInfo: function(){
        wx.navigateTo({
            url: "/pages/edit_info/edit_info"
        })
    },

    //学生-待学常规课
    toStudentCourseNormal: function(e){
        wx.navigateTo({
            url: '/pages/student_course_normal/student_course_normal'
        }) 
    },

    //学生-待学常规课
    toStudentCourseInterest: function(e){
        wx.navigateTo({
            url: '/pages/student_course_interest/student_course_interest'
        }) 
    },

    //学生-已学习
    toStudentCourseAttendance: function(e){
        wx.navigateTo({
            url: '/pages/student_course_attendance/student_course_attendance'
        })
    },

    //教师-待授常规课
    toTeacherCourseNormal: function(e){
        wx.navigateTo({
            url: '/pages/teacher_course_normal/teacher_course_normal'
        }) 
    },

    //教师-待授常规课
    toTeacherCourseInterest: function(e){
        wx.navigateTo({
            url: '/pages/teacher_course_interest/teacher_course_interest'
        }) 
    },

    //教师-已授课
    toTeacherCourseAttendance: function(e){
        wx.navigateTo({
            url: '/pages/teacher_course_attendance/teacher_course_attendance'
        })
    },

    //班主任-查看考勤
    toClassmasterAttendance: function(e){
      wx.navigateTo({
          url: '/pages/classmaster_attendance/classmaster_attendance'
      })
    },

    //班主任-查看学生
    toClassmasterStudent: function(e){
      wx.navigateTo({
          url: '/pages/classmaster_student/classmaster_student'
      })
    },

    //班主任-查看考勤
    toSchoolmasterAttendance: function(e){
      wx.navigateTo({
          url: '/pages/schoolmaster_attendance/schoolmaster_attendance'
      })
    },

    //校长-查看学生
    toSchoolmasterStudent: function(e){
      wx.navigateTo({
          url: '/pages/schoolmaster_student/schoolmaster_student'
      })
    },

     //校长-查看班级
     toSchoolmasterClass: function(e){
      wx.navigateTo({
          url: '/pages/schoolmaster_class/schoolmaster_class'
      })
    },

    //显示弹窗
    showSchool: function () {
        this.getSchool();
        this.setData({
            school_modal: 'show'
        }) 
    },

    //选择学校
    selectSchool(e){
        this.setData({
            select_school_id: e.currentTarget.dataset.school_id,
            select_school_name: e.currentTarget.dataset.school_name
        })
    },

     //获取学校列表
     getSchool: function () {
        let _this = this;
        app._get('/api/school/getSchool', null, function (response) {
            _this.setData({
                school: response.data.data
            })
        })
    },
    
    //确定学校后
    confirmSchool: function () {
        let _this = this;
        app._post('/api/login/user/updateSchool', {
            school_id: _this.data.select_school_id,
            school_name: _this.data.select_school_name
        }, function (response) {
            console.log(response)
            if (response.data.code == 200) {
                _this.setData({
                    school_modal: '',
                    school_id: _this.data.select_school_id,
                    school_name: _this.data.select_school_name,
                })
                app.globalData.school_id=_this.data.select_school_id;
                app.globalData.school_name=_this.data.select_school_name;
                _this.getUser();
            }
        })
    },

    //显示弹窗
    showRole: function () {
        this.getSchool();
        this.setData({
            role_modal: 'show'
        }) 
    },

    //选择学生
    checkStudent(){
        this.setData({
            select_role: 1
        })
    },

    //选择教师
    checkTeacher(){
        this.setData({
            select_role: 2
        })
    },

     //选择班主任
     checkClassmaster(){
        this.setData({
            select_role: 5
        })
    },
    
     //选择校长
     checkSchoolmaster(){
        this.setData({
            select_role: 6
        })
    },
    
    
    //确定角色后
    confirmRole(){
        this.setData({
            role: this.data.select_role
        })

        let _this = this;
        app._post('/api/login/user/updateRole', {
            role: _this.data.select_role
        }, function (response) {
            console.log(response)
            if (response.data.code == 200) {
                _this.setData({
                    role_modal: '',
                    role: _this.data.select_role
                })
                app.globalData.role=_this.data.select_role
                _this.getUser();
            }
        })
    },


    // 订阅消息
    subscribeMsg(){
      wx.requestSubscribeMessage({
        tmplIds: [
          'D4_2XDhID9MN1k--WMMjYxi4ERqy-o2QSO7NJLF2uwY',// 教师通知
          '1dcO1PWjx6q4-N8zbqsxXcvWn-L3dPF942MG842Xbrw',// 考勤通知
        ],complete (res) {
          wx.showToast({
            icon: 'none',
            title: '操作成功',
          })
        }
      })
    },

    // 消息详情
    messageDetail(e) {
      console.log(e)
      let mid = e.currentTarget.dataset.mid
      let mtitle = e.currentTarget.dataset.mtitle
      let mdesc = e.currentTarget.dataset.mdesc
      this.readMessage(mid)
      wx.showModal({
        title: mtitle,
        content: mdesc,
        showCancel: false,
        confirmText: '确定',
        success: function (res) {
            if (res.confirm) {
                console.log('用户点击了确定')
            }
        }
      })
    },

    // 获取消息列表
    getMessageList(){
      let _this = this
      app._get('/api/message/getList', {
        
      }, function (response) {
          console.log(response)
          if (response.data.code == 200) {
              _this.setData({
                message_list: response.data.data,
              })
          }
      })
    },

    // 读消息
    readMessage(id){
      app._post('/api/message/read', {
        id: id
      }, function (response) {
          console.log(response)
          if (response.data.code == 200) {
              
          }
      })
    },

    //切换
    switchStudent:function (){
        let authinfo = wx.getStorageSync('authinfo');
        const {member_id} = authinfo;
        wx.navigateTo({
            url: '/pages/bind/bind?member_id='+member_id,
            events: {
                // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
                acceptDataFromOpenedPage: function(data) {
                    console.log(data)
                },
                someEvent: function(data) {
                    console.log(data)
                }
            },
            success: function(res) {
                // 通过eventChannel向被打开页面传送数据
                res.eventChannel.emit('acceptDataFromOpenerPage', { data: 'test' })
            }
        })
    }

})